//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeValue: "获取验证码",
    flag: false,
    phone: "",

  },
  // 
  phoneTap: function (e) {
    console.log(e);
    this.setData({
      phone: e.detail.value,
    });
  },
  // 输入验证码
  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
    console.log(e);
  },
  // 获取验证码
  getCode: function () {
    var that = this;
    var phone = this.data.phone;
    var codeValue = this.data.codeValue;
    // var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (this.data.phone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      wx.request({
        url: baseJs.api_Utils + "CreateVerificationCode" + "?" + "MobilePhone=" + phone + "&t=" + "3",
        success: function (res) {
          var json = res.data;
          console.log(json)
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codeValue: '重新发送',
                flag: false
              })

            } else {
              that.setData({
                flag: true,
                codeValue: num + " s"
              })
            }
          }, 1000)
        }
      })
    }
  },
  // 确认修改
  formSubmit: function (e) {
    console.log(e);
    var that = this;
    if (e.detail.value.password != e.detail.value.surePassword) {
      wx.showToast({
        title: "两次密码不一致",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=FindMyPwd&Phone=" + e.detail.value.phone + "&NewPwd=" + e.detail.value.surePassword + "&Verification=" + e.detail.value.code),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 1500
          });
          setTimeout(function () {
            wx.navigateTo({
              url: '/packageA/login/login',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
})